package com.bootdo.thread;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @Auther: liuj
 * @Date: 2019/5/12 21:20
 * @Description:
 */
public class ExecutorServiceTest {
    private final int NUMBER = 4;

    public void ExecutorService(){
        ExecutorService pool = Executors.newFixedThreadPool(NUMBER);
        ArrayList<Future<String>> futures = new ArrayList<>();

        for(int i=0; i<10; i++){
            AThread t = new AThread(i);
            Future<String> f = pool.submit(t);
            futures.add(f);
        }


        System.out.println("开始获取结果。。。");
        for(Future<String> f1 : futures){
            try {
                System.out.println(f1.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        System.out.println("结束");

        pool.shutdown();
    }

    public static void main(String[] args){
        new ExecutorServiceTest().ExecutorService();
    }

    public class AThread implements Callable<String>{

        private int id;

        public AThread(int id){
            this.id = id;
        }

        @Override
        public String call() throws Exception {
            System.out.println("线程id : " + id + "运行");
            Thread.sleep(3000);

            System.out.println("线程id : " + id + "结束");
            return "返回值：" + id;
        }
    }
}
